module Alglib
    class Logit
        attr_reader :model, :cases, :ivars
        # Creates a Logit object based on a Matrix
        # EXAMPLE: 
        #   require 'alglib'
        #   matrix=Matrix[[2,3,4],[2,5,5],[1,5,3],[4,6,5]]
        #   lr=Alglib::LinearRegression.build_from_matrix(matrix)
        #     => #<Alglib::LinearRegression:0x7ffaf6c05dc0 @model=#<Alglib_ext::LinearModel:0x7ffaf6c05e60>, @cases=4, @report=#<Alglib_ext::LrReport:0x7ffaf6c05e10>, @ivars=2>
        #     lr.coeffs
        #     => [0.585714285714286, -0.0142857142857142]

        def self.build_from_matrix(matrix)
            raise "Argument should be a matrix" unless matrix.is_a? Matrix
            cases=matrix.row_size
            ivars=matrix.column_size-1
            lm=Alglib_ext::LogitModel.new
            lr=Alglib_ext::MNLReport.new
            nclasses=matrix.column(matrix.column_size-1).to_a.uniq.size
            am=matrix.to_alglib_matrix
            Alglib_ext::mnltrainh(am,cases,ivars,nclasses,lm,lr)
            self.new(lm,lr,cases,ivars,nclasses)
        end
        # Use with care...
        def initialize(lm,lr,cases,ivars,nclasses)
            @model=lm
            @report=lr
            @cases=cases
            @ivars=ivars
            @nclasses=nclasses
        end
        def process(vector)
            Alglib_ext.mnlprocess(@model,vector)
        end
        def unpack
            a2d=Alglib_ext::Real2dArray.new
            Alglib_ext::mnlunpack(@model, a2d,@ivars,@nclasses);
            Alglib.real2darray_to_array(a2d)
        end

    end
end
